<template>
  <div>
    <el-button @click="show=true">+ 选择商品</el-button>

    {{books}}

    <!-- 弹出框 -->
    <el-dialog title="选择商品" :visible.sync="show">

      <!-- 切换框 -->
      <el-tabs value="class">
        <el-tab-pane label="课程" name="class">
          <Class></Class>
        </el-tab-pane>
        <el-tab-pane label="图书" name="book">
          <Book @books="getBook"></Book>
        </el-tab-pane>
      </el-tabs>

      <!-- 底部按钮 - footer 插槽 -->
      <template slot="footer">
        <el-button @click="show=false">取消</el-button>
        <el-button @click="ok">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import Book from './goods/Book'
import Class from './goods/Class'
export default {
  // 注册组件
  components: {
    Book,
    Class
  },
  data() {
    return {
      show: false,
      books: {}, // 真正使用到的数据
      tmpBooks: {} // 临时保存子组件传过来的值
    }
  },
  methods: {
    // 拿到子组件的值
    getBook(data) {
      // 把数据保存到 data 中的 books
      this.tmpBooks = data
    },
    ok() {
      // 把临时保存的保存到books
      this.books = this.tmpBooks

      // 关闭弹出框
      this.show = false
    }
  }
}
</script>
